package com.tangly.codeing.utils;

import java.util.Arrays;
import java.util.Map;

/**
 * 分页工具类
 *
 * @author tangbing
 * @date 2019/4/29
 */
public class PageInfo {
    /**
     * limit sql起始数值(由currentPage计算得出)
     */
    private Integer startRecord;
    /**
     * 当前页
     */
    private Integer currentPage;
    /**
     * 一页多少条数据
     */
    private Integer pageSize;
    /**
     * 总页数(由totalRecord计算得出)
     */
    private Integer totalPages;
    /**
     * 总条数
     */
    private Integer totalRecord;
    /**
     * 返回对象
     */
    private Object resultObject;
    /**
     * 排序字段
     */
    private String orderColumn;
    /**
     * 升/降序排列
     */
    private String orderSort;
    /**
     * where 查询
     */
    private Map<String, Object> whereMap;
    /**
     * 分组
     */
    private String groupColumns;

    /**
     * es关键字
     */
    private String keyword;
    /**
     * es索引名称
     */
    private String indexName;
    /**
     * es搜索字段（逗号分隔）
     */
    private String fields;

    @Override
    public String toString() {
        StringBuffer whereMapBuffer = new StringBuffer();
        if (whereMap != null) {
            for (Map.Entry<String, Object> entry : whereMap.entrySet()) {
                if (entry.getKey().equals("branchIdArray") || entry.getKey().equals("orderStatusArray")) {
                    whereMapBuffer.append(entry.getKey() + ":" + Arrays.toString((String[]) entry.getValue()) + ",");
                } else {
                    whereMapBuffer.append(entry.getKey() + ":" + entry.getValue() + ",");
                }
            }
        }

        return new StringBuilder()
                .append("startRecord:" + startRecord + ",")
                .append("currentPage:" + currentPage + ",")
                .append("pageSize:" + pageSize + ",")
                .append("totalPages:" + totalPages + ",")
                .append("totalRecord:" + totalRecord + ",")
                .append("orderColumn:" + orderColumn + ",")
                .append("orderSort:" + orderSort + ",")
                .append("groupColumns:" + groupColumns + ",")
                .append("whereMap:{" + whereMapBuffer + "},")
                .append("resultObject:" + resultObject)
                .toString();
    }

    public Integer getStartRecord() {
        if (null == getCurrentPage()) {
            return 0;
        }
        return (getCurrentPage() - 1) * pageSize;
    }

    public void setStartRecord(Integer startRecord) {
        this.startRecord = startRecord;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Object getResultObject() {
        return resultObject;
    }

    public void setResultObject(Object resultObject) {
        this.resultObject = resultObject;
    }

    public Integer getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(Integer totalRecord) {
        this.totalRecord = totalRecord;
    }

    public String getOrderColumn() {
        return orderColumn;
    }

    public void setOrderColumn(String orderColumn) {
        this.orderColumn = orderColumn;
    }

    public String getOrderSort() {
        return orderSort;
    }

    public void setOrderSort(String orderSort) {
        this.orderSort = orderSort;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getTotalPages() {
        if (null == getPageSize() || null == getTotalRecord()) {
            return 1;
        }
        int restRecord = getTotalRecord() % getPageSize();
        int tempPages = getTotalRecord() / getPageSize();
        if (restRecord == 0) {
            return Integer.valueOf(tempPages);
        } else {
            return Integer.valueOf(tempPages + 1);
        }
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Map<String, Object> getWhereMap() {
        return whereMap;
    }

    public void setWhereMap(Map<String, Object> whereMap) {
        this.whereMap = whereMap;
    }

    public String getGroupColumns() {
        return groupColumns;
    }

    public void setGroupColumns(String groupColumns) {
        this.groupColumns = groupColumns;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getIndexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    public String getFields() {
        return fields;
    }

    public void setFields(String fields) {
        this.fields = fields;
    }
}
